package day3.Bum2.Bum;

/**
 * ClassName：Customer
 *
 * @Author：小鱼
 * @Create：2025-08-20 11:48
 */
public class Customer2 extends Thread{
    private Bum bum;
    public Customer2(String name, Bum bum) {
        super(name);
        this.bum = bum;
    }
    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {
            //同步
            synchronized (bum) {
                //根据flag判断包子是否存在，如果不存在则线程等待
                while (bum.flag!=3) {
                    try {
                        bum.wait();
                    } catch (InterruptedException e) {

                        e.printStackTrace();
                    }
                }
                System.out.println("第" + i + "次，" + this.getName() + " 开始吃包子...");
                bum.num--;
                System.out.println("包子数量: " + bum.num );
                //消费完成，修改flag存在标识
                bum.flag = 0;
                bum.notifyAll();
            }
        }
    }
}
